<template>
  <a-modal
    title="通知公告"
    :width="drawerWidth"
    :visible="visible"
    :destroyOnClose="true"
    @cancel="handleCancel"
    :footer="null"
    cancelText="关闭">
    <div class="table-page-search-wrapper">
      <a-form-model
        ref="ruleForm"
        layout="inline"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item
              label="标题"
              :maxLength="50">
              <a-input placeholder="请输入标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
        <s-table
          ref="table"
          rowKey="id"
          size="middle"
          :scroll="{ x: 1300 }"
          :columns="columns"
          :data="loadData">
          <template slot="action" slot-scope="text, record">
            <a @click="handelView(record)" v-wx-role="51505">
              <a-icon type="eye"/>
              查看</a>
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.sncId)" v-wx-role="51504">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </s-table>
        <div class="table-operator">
          <a-button type="primary" @click="readAll" icon="book">全部标注已读</a-button>
        </div>
        <my-notice-view ref="modalView"></my-notice-view>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>

  import { STable, Ellipsis } from '@/components'
  import { deleteAction, getAction, postAction } from '@/api/manage'
  import { baseMixin } from '@/store/app-mixin'
  import MyNoticeView from '../../views/account/settings/MyNoticeView'

  export default {
    name: 'MyNoticeList',
    mixins: [baseMixin],
    components: { STable, Ellipsis, MyNoticeView },
    data () {
      return {
        drawerWidth: document.body.clientWidth - 200,
        visible: false,
        formInfo: {},
        // 查询条件
        queryParam: {
          title: '',
          msgCategory: 0
        },
        // 表头
        columns: [
          {
            title: '标题',
            dataIndex: 'title'
          },
          {
            title: '消息类型',
            dataIndex: 'msgCategory',
            customRender: (text) => this.$sysDictFilter(text, 'msgCategory')
          },
          {
            title: '发布人',
            dataIndex: 'createBy'
          },
          {
            title: '优先级',
            dataIndex: 'priority',
            customRender: (text) => this.$sysDictFilter(text, 'priority')
          },
          {
            title: '发布时间',
            dataIndex: 'createTime',
            customRender: (text) => this.$momentFormat(text, 'YYYY-MM-DD')
          },
          {
            title: '阅读状态',
            dataIndex: 'readFlag',
            customRender: (text) => this.$sysDictFilter(text, 'readFlag')
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          console.log('loadData.parameter', parameter)
          return postAction(this.url.list, Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },
        dict: '',
        url: {
          list: '/system/sysNotice/myNoticeList',
          delete: '/system/sysNotice/myNoticeDelete',
          readAll: '/system/sysNotice/myNoticeViewAll'
        }
      }
    },
    computed: {},
    methods: {
      add () {
        this.visible = true
      },
      // 全部标记已读
      readAll () {
        getAction(this.url.readAll, {}).then((res) => {
          this.$message.success(res.content)
          this.$refs.table.refresh(true)
        })
      },
      handleCancel () {
        this.$emit('ok')
        this.visible = false
      },
      // 查看字典数据
      handelView (record) {
        this.$refs.modalView.show(record)
      },
      // 删除
      handleDelete (record) {
        deleteAction(this.url.delete, { ids: record }).then((resp) => {
          this.$message.success('删除成功!')
          this.$refs.table.refresh(true)
        })
      }
    },
    watch: {}
  }
</script>
